package com.lwc.controller;

import com.lwc.bean.Enterprise;
import com.lwc.bean.Xufei;
import com.lwc.bean.params.EnterpriseQuery;
import com.lwc.common.bean.PageBean;
import com.lwc.common.bean.ResultBean;
import com.lwc.common.controller.BaseController;
import com.lwc.common.crud.IUpdate;
import com.lwc.common.exceotion.BusinessexCeption;
import com.lwc.mapper.AccountDetailMapper;
import com.lwc.service.EnterpriseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Past;
import java.util.Map;

@RestController
@Api(tags = "企业管理")
@RequestMapping("/api")
@CrossOrigin
public class EnterpriseController extends BaseController {

@Autowired
    EnterpriseService enterpriseService;

    @ApiOperation("新增接口")
    @PostMapping("/enterprise")
    public ResultBean save(@Validated  @RequestBody Enterprise enterprise, BindingResult result){
        validate(result);
int rs=enterpriseService.save(enterprise);
return  rs==1?success("新增成功"):fail("新增失败");
    }


    @ApiOperation("列表查询")
@PostMapping("/enterprise/list")
    public ResultBean<PageBean>query( @RequestBody EnterpriseQuery query){
        PageBean pageBean=enterpriseService.query(query.toMap());
return  success("企业列表",pageBean);
    }

@ApiOperation("更改接口")
    @PutMapping("/enterprise/add")
    public  ResultBean update(@Validated(IUpdate.class) @RequestBody Enterprise enterprise, BindingResult result){
        //检验
        validate(result);
        int rs=enterpriseService.update(enterprise);
        return getResultupdate(rs);
    }

    @ApiImplicitParam(name = "id",value = "企业id",dataTypeClass = Integer.class)
    @ApiOperation("删除接口")
    @DeleteMapping("/enterprise/delete")
public  ResultBean delete(@RequestParam( name = "id",required = true ) int id){
        int rs=enterpriseService.delete(id);
        return rs==1?success("删除成功"):fail("删除失败");
}

@ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "企业id", required = true, dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "empNum",value = "可创建的员工数",required = true,dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "accountNum",value = "可创建的套数",required = true,dataTypeClass = Integer.class)
})
@ApiOperation("授予权限")
@PutMapping("enterprise/auth")
public  ResultBean auth(@RequestParam int id,int empNum,int accountNum){

        int rs=enterpriseService.auth(id, empNum, accountNum);
        return rs==1?success("保存成功"):fail("保存失败");
}



//续费
    @Transactional
    @ApiOperation("续费接口")
    @PutMapping("/enterprise/xufei")
    public ResultBean xufei(@Validated @RequestBody  Xufei xufei,BindingResult result){
        //检验后端
        validate(result);
        int rs=enterpriseService.xufei(xufei);
        return  rs==1?success("续费成功"):fail("续费失败");
    }


    @ApiOperation("查看企业资金信息")
    @GetMapping("/enterprise/{id}")
public ResultBean xinxi(@PathVariable(name = "id")  int id){
       Map map= enterpriseService.xinxi(id);
       return  success("薪金信息",map);
}


@ApiOperation("导出企业")
@PostMapping("/enterprise/expose")
public  ResultBean expose(@RequestBody  Map map){
String url= enterpriseService.export(map);
        return success("导出企业",url);
}



}
